﻿/*
____
 L|ypps Web controls for ASP.NET

Copyright (c) 2009 Alessandro Zifiglio, http://www.Typps.com
EMail: alessandro@Typps.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace Typps
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Drawing.Design;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Web.UI;

    [DataContract(Name="toolBarItem", Namespace="")]
    public class ToolBarItem : IStateManager
    {
        #region Fields

        bool defaultValue = false;
        private bool isTrackingViewStateValue;
        private StateBag viewStateValue;

        #endregion Fields

        #region Constructors

        public ToolBarItem()
            : this(0, string.Empty, string.Empty)
        {
            // overloaded constructor
        }

        public ToolBarItem(int marginLeft, string name, string toolTip)
        {
            this.MarginLeft = marginLeft;
            this.Name = name;
            this.ToolTip = toolTip;
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// Gets/Sets a break in the toolbar. A break is an item separator, a vertical line used a toolbar delimiter
        /// </summary>
        [DataMember(Name="divider"),
        Category("Behavior"),
        DefaultValue(false),
        NotifyParentProperty(true),
        Description("Gets or Sets a divider after this item")]
        public bool Break
        {
            get
            {
                object o = (object)ViewState["Break"];
                return (o == null) ? false : (bool)o;
            }
            set
            {
                defaultValue = false;
                ViewState["Break"] = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the ToolBarItem is enabled. Depending on the IconsMode, if set to Icons(the default), 
        /// the ToolBarItem won't show in the ToolBar, while if the IconsMode is ImageSprites, the disabled imagesprite 
        /// is displayed showing that this ToolBarItem is disabled and appears dimmed.
        /// </summary>
        [DataMember(Name = "enabled"),
        Category("Behavior"),
        DefaultValue(true),
        NotifyParentProperty(true),
        Description("Gets or Sets if this icon is Enabled")]
        public bool Enabled
        {
            get
            {
                object o = (object)ViewState["Enabled"];
                return (o == null) ? true : (bool)o;
            }
            set
            {
                defaultValue = false;
                ViewState["Enabled"] = value;
            }
        }

        /// <summary>
        /// Gets/Sets a groupName value that makes all toolBarItems with the same groupname to function as radio buttons. 
        /// This means only one item in the group can be toggled at a time.
        /// </summary>
        [DataMember(Name = "groupName"),
        Category("Behavior"),
        DefaultValue(true),
        NotifyParentProperty(true),
        Description("Makes this item function like a radiobutton")]
        public string GroupName
        {
            get
            {
                object o = (object)ViewState["GroupName"];
                return (o == null) ? string.Empty : (string)o;
            }
            set
            {
                defaultValue = false;
                ViewState["GroupName"] = value;
            }
        }

        /// <summary>
        /// Gets/Sets the Url to the Icon displayed for this ToolBarItem.
        /// </summary>
        [DataMember(Name = "icon"),
        Editor(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(UITypeEditor)),
        DefaultValue(""),
        NotifyParentProperty(true)]
        public string Icon
        {
            get
            {
                return (ViewState["Icon"] == null) ? string.Empty : (string)ViewState["Icon"];
            }
            set
            {
                defaultValue = false;
                ViewState["Icon"] = value;
            }
        }

        /// <summary>
        /// Gets/Sets a value indicating the left margin, the spacing between this ToolBarItem and its left edge. This is particularly useful
        /// when working in ImageSprites mode.
        /// </summary>
        [DataMember(Name = "marginLeft"),
        Bindable(false),
        Category("Behavior"),
        DefaultValue(0),
        NotifyParentProperty(true),
        Description("Left margin spacing of your icon with respect to left corner of bar, if its the first icon or right corner of the icon before it.")]
        public int MarginLeft
        {
            get
            {
                object o = (object)ViewState["MarginLeft"];
                return (o == null) ? 0 : (int)o;
            }
            set
            {
                defaultValue = false;
                ViewState["MarginLeft"] = value;
            }
        }

        /// <summary>
        ///Get/Sets a command name value to help identify this ToolBarItem since multiple 
        ///toolbaritems exist in a toolbarset, setting this property allows you to distinguish 
        ///or rather identify one command from another when subscribing to the 
        ///clientside event handler "ClientSideToolBarItemClick".
        /// </summary>
        [DataMember(Name = "name"),
        Bindable(false),
        Category("Behavior"),
        DefaultValue(""),
        NotifyParentProperty(true),
        Description("Gets or Sets the command to call on execCommand method exposed through the dom")]
        public string Name
        {
            get
            {
                object o = (object)ViewState["Name"];
                return (o == null) ? string.Empty : o.ToString();
            }
            set
            {
                defaultValue = false;
                ViewState["Name"] = value;
            }
        }

        /// <summary>
        /// Gets/Sets a value that indicates whether the ToolBarItem should be a toggle enabled Item. 
        /// Toggle enabled items are highlighted with a pressed button effect when clicked indicating an active state
        /// </summary>
        [DataMember(Name = "toggleEnabled"),
        Category("Behavior"),
        DefaultValue(false),
        NotifyParentProperty(true),
        Description("Allows this item in the toolbar to be toggled")]
        public bool ToggleEnabled
        {
            get
            {
                object o = (object)ViewState["ToggleEnabled"];
                return (o == null) ? false : (bool)o;
            }
            set
            {
                defaultValue = false;
                ViewState["ToggleEnabled"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the text displayed when the mouse pointer hovers over the ToolBarItem. Since all Items in the ToolBar are represented by Icons, a short description
        /// in the form of a ToolTip is always helpful.
        /// </summary>
        [DataMember(Name = "title"),
        Bindable(false),
        Category("Behavior"),
        DefaultValue(""),
        NotifyParentProperty(true),
        Description("Gets or Sets the tooltip for this icon")]
        public string ToolTip
        {
            get
            {
                object o = (object)ViewState["ToolTip"];
                return (o == null) ? string.Empty : o.ToString();
            }
            set
            {
                defaultValue = false;
                ViewState["ToolTip"] = value;
            }
        }

        bool IStateManager.IsTrackingViewState
        {
            get
            {
                return isTrackingViewStateValue;
            }
        }

        internal bool Defaults
        {
            get
            {
                return defaultValue;
            }
            set
            {
                defaultValue = value;
            }
        }

        private StateBag ViewState
        {
            get
            {
                if (viewStateValue == null)
                {
                    viewStateValue = new StateBag(false);
                    if (isTrackingViewStateValue)
                    {
                        ((IStateManager)viewStateValue).TrackViewState();
                    }
                }
                return viewStateValue;
            }
        }

        #endregion Properties

        #region Methods

        void IStateManager.LoadViewState(object savedState)
        {
            this.LoadViewState(savedState);
        }

        object IStateManager.SaveViewState()
        {
            if (Defaults)
                return null;

            return this.SaveViewState();
        }

        void IStateManager.TrackViewState()
        {
            isTrackingViewStateValue = true;
            if (viewStateValue != null)
                ((IStateManager)viewStateValue).TrackViewState();
        }

        internal void LoadViewState(object savedState)
        {
            object[] states = (object[])savedState;
            ((IStateManager)ViewState).LoadViewState(states[0]);
        }

        internal object SaveViewState()
        {
            object[] states = new object[1];
            states[0] = (viewStateValue != null) ? ((IStateManager)viewStateValue).SaveViewState() : null;
            return states;
        }

        internal void SetDirty()
        {
            viewStateValue.SetDirty(true);
        }

        #endregion Methods
    }
}